﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.CodeDom;

namespace Generator.Tools
{
    public partial class ConvertServerControllerToMonoTouch : ToolsBase
    {
        public ConvertServerControllerToMonoTouch()
        {
            InitializeComponent();
        }

        private void ConvertServerControllerToMonoTouch_Load(object sender, EventArgs e)
        {

        }

        private void LoadAssembly(string partialName)
        {
            // GAC or WEBと同一フォルダ(bin)よりPartialName取得
            //Assembly asm = Assembly.LoadWithPartialName(partialName);
            // アセンブリ読み込み
            Assembly asm = Assembly.LoadFrom(partialName);

            if (asm == null)
            {
                // 起動対象となるアセンブリ名が見つかりませんでした。
                richTextBox1.AppendText("can not load this assembly");
                return;
            }
            var resultDic = new SortedDictionary<string, List<string>>();

            var tps = asm.GetTypes();
            foreach (var tp in tps)
            {
                CreateClassType(resultDic, tp);
                //break;
            }

            var keys = resultDic.Keys;
            //foreach (var key in keys)
            //{
            //    richTextBox1.AppendText(string.Format("using {0};", key));
            //    richTextBox1.AppendText(Environment.NewLine);
            //}

            //richTextBox1.AppendText(Environment.NewLine);
            //richTextBox1.AppendText(Environment.NewLine);

            foreach (var key in keys)
            {
                var list = resultDic[key];
                if (list.Any())
                {
                    richTextBox1.AppendText(string.Format("namespace {0}", key));
                    richTextBox1.AppendText(Environment.NewLine);
                    richTextBox1.AppendText("{");
                    richTextBox1.AppendText(Environment.NewLine);

                    list.ForEach(m =>
                        {
                            richTextBox1.AppendText("\t" + m);
                            richTextBox1.AppendText(Environment.NewLine);
                        });

                    //richTextBox1.AppendText(Environment.NewLine);
                    richTextBox1.AppendText("}");
                    richTextBox1.AppendText(Environment.NewLine);
                    richTextBox1.AppendText(Environment.NewLine);
                }
            }
        }


        private void CreateClassType(SortedDictionary<string, List<string>> resultDic, Type tp)
        {
            if (!tp.IsPublic)
            {
                return;
            }

            var tpns = tp.Namespace;
            if (string.IsNullOrEmpty(tpns))
                tpns = typeof(System.Object).FullName;

            if (!resultDic.ContainsKey(tpns))
            {
                resultDic.Add(tpns, new List<string>());
            }

            var stringBuilder = new StringBuilder();
            //string sparator = ":";

            stringBuilder.Append("public ");
            stringBuilder.Append("class ");
            stringBuilder.Append(tp.Name);
            if (tp.BaseType != null && ( string.IsNullOrEmpty( tp.BaseType.FullName) || !tp.BaseType.FullName.Equals(typeof(System.Object).FullName)))
            {
                var ns = tp.BaseType.Namespace;
                if (string.IsNullOrEmpty(ns))
                    ns = typeof(System.Object).FullName;

                if (!resultDic.ContainsKey(ns))
                {
                    resultDic.Add(ns, new List<string>());
                }

                //stringBuilder.Append(sparator + tp.BaseType.Name);
                //sparator = ",";
            }
            var interfaces = tp.GetInterfaces();
            foreach (var t in interfaces)
            {
                var ns = t.Namespace;
                if (string.IsNullOrEmpty(ns))
                    ns = typeof(System.Object).FullName;

                if (!resultDic.ContainsKey(ns))
                {
                    resultDic.Add(ns, new List<string>());
                }

                //stringBuilder.Append(sparator + t.Name);
                //sparator = ",";
            }

            stringBuilder.Append("{}");
            resultDic[tpns].Add(stringBuilder.ToString());
        }

        private void textBox2_KeyDown(object sender, KeyEventArgs e)
        {
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var res= openFileDialog1.ShowDialog();
            if (res == DialogResult.OK)
            {
                textBox2.Text = openFileDialog1.FileName;
                LoadAssembly(textBox2.Text);
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            LoadAssembly(textBox2.Text);
        }
    }
}
